package com.fjd.service.impl;

import com.fjd.dto.AfterSaleDTO;
import com.fjd.entity.AfterSale;
import com.fjd.mapper.AfterSaleMapper;
import com.fjd.mapper.AfterSalesImageMapper;
import com.fjd.service.AfterSaleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@Service
public class AfterSaleServiceImpl implements AfterSaleService {
    @Autowired
    AfterSaleMapper afterSaleMapper;
    @Autowired
    AfterSalesImageMapper afterSalesImageMapper;
    @Override
    public List<AfterSale> list(){
        return afterSaleMapper.list();
    }
    @Override
    public AfterSale findById(String id) {
        return afterSaleMapper.findById(id);
    }

    @Transactional
    @Override
    public void insertAfterSale(AfterSaleDTO afterSaleDTO) {
        AfterSale afterSale=new AfterSale();
        BeanUtils.copyProperties(afterSaleDTO, afterSale);

        afterSale.setId(UUID.randomUUID().toString().replace("-",""));
        afterSale.setGenerateTime(LocalDateTime.now());
        afterSale.setOrderId(afterSaleDTO.getOrderId());
        afterSale.setReason(afterSaleDTO.getReason());
        afterSale.setReasonDetail(afterSaleDTO.getReasonDetail());
        afterSale.setAmount(afterSaleDTO.getAmount());
        afterSale.setAftersalesState("待处理");
        afterSale.setType(afterSaleDTO.getType());

        afterSaleMapper.insertAfterSale(afterSale);
        //添加评价图片
        if(afterSaleDTO.getImageList().size()!=0 && afterSaleDTO.getImageList() != null){

            setAssessImageList(afterSale.getId(), afterSaleDTO.getImageList());
        }
    }
    public void setAssessImageList(String afterSaleId, List<String> imageList){
        afterSalesImageMapper.insert(afterSaleId, imageList);
    }

    @Override
    public void updateAfterSale(String id, String state, String note) {
        afterSaleMapper.updateAftersalesState(id,state,note);
    }

    @Override
    public List<String> getImage(String id){
        return afterSalesImageMapper.getByAfterSaleId(id);
    }

    @Override
    public void deleteAfterSale(String id) {
        afterSaleMapper.deleteAfterSale(id);
        afterSalesImageMapper.deleteAfterSaleImage(id);
    }
}
